package tests;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;

public class EditPage extends Utils {

    public static String url = "http://127.0.0.1:9580/?";
    public EditPage() {
        super(url);
    }

    public void  EditSuc() throws InterruptedException, IOException {
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post")).click();
        getScreenShot(getClass().getName());
        driver.findElement(By.cssSelector("#article_post_title")).sendKeys("1111");

        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(2));
//        wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#edit-article > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre")));
        //无法输入博客内容怎么办？
        //1）本身有默认内容不需要实现
        //2）通过键盘操作实现
        WebElement ele = driver.findElement(By.cssSelector("#edit-article > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre"));

        Actions actions = new Actions(driver);
        //perform作用：为了在页面看到效果
        actions.click(ele).perform();
//        actions.keyDown(Keys.DELETE).perform();
        actions.moveToElement(ele).sendKeys("键盘鼠标操作输入博客内容111111").perform();
        getScreenShot(getClass().getName());

        // 定位目标元素
        WebElement target = driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div.card-footer.bg-transparent.mt-auto > div > a"));
        Thread.sleep(3000);
        Actions subAction = new Actions(driver);
        // 创建 Actions 对象
        subAction.moveToElement(target).click().perform();

        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
        String res = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2")).getText();
        getScreenShot(getClass().getName());
        Thread.sleep(100);
        getScreenShot(getClass().getName());
        assert res.equals("成功");

    }
}
